//
//  GPSExerciseHeartChartTableViewCell.swift
//  DaFit
//
//  Created by Paul on 2022/10/27.
//  Copyright © 2022 sylar. All rights reserved.
//

import UIKit
import MYModule

open class GPSTrainingHeartChartTableViewCell: TableViewCell {
    
    open var trainingModel: TrainingModel? {
        
        didSet {
            
            
            
            guard let trainingModel = trainingModel else {
                
               let start = "--" + "gps_training_start".language
               let end = "--" + "gps_training_end".language
                let config = HeartRateChartLineConfig(list: [], startTime: start, endTime: end)
                averageView.config = config
                return
            }
         
            var start = Date(timeIntervalSince1970: TimeInterval(trainingModel.startTime)).toString(.custom("MM/dd HH:mm"))
            var end = Date(timeIntervalSince1970: TimeInterval(trainingModel.endTime)).toString(.custom("MM/dd HH:mm"))
            start = start + "gps_training_start".language
            end = end + "gps_training_end".language
            
            let heartsAvg = Int(trainingModel.getAvgHR())
            
            let config = HeartRateChartLineConfig(avg: heartsAvg, high: trainingModel.getMaxHR(), low: trainingModel.getMinHR(), list: trainingModel.heartRates, startTime: start, endTime: end)
            averageView.config = config
            
        }
        
    }
    
    private let bgView = View()
    
    public let averageView = HeartRateChartLineView()
    
    

    open override func makeUI() {
        super.makeUI()
        
        contentView.backgroundColor =  UIColor.darkBackground
        bgView.backgroundColor = UIColor.background
        bgView.cornerRadius = 18.0
        averageView.cornerRadius = 18.0
        
        averageView.startSleepTimeLabel.text = "--" + "gps_training_start".language
        averageView.endSleepTimeLabel.text = "--" + "gps_training_end".language
        
        averageView.fillChartView.titleLabel.text = "heart_rate_average_title".language
        averageView.fillChartView.unitLabel.text = "heart_rate_bpm".language
        
        averageView.fillChartView.maxHRVView.iconImageView.image = UIImage(named: "ic_sleep_max")
        averageView.fillChartView.maxHRVView.unitLabel.text = "heart_rate_bpm".language
        averageView.fillChartView.maxHRVView.descLabel.text = "sleep_max_hr".language

        averageView.fillChartView.minHRVView.iconImageView.image = UIImage(named: "ic_sleep_min")
        averageView.fillChartView.minHRVView.unitLabel.text = "heart_rate_bpm".language
        averageView.fillChartView.minHRVView.descLabel.text = "sleep_min_hr".language
        
        
        averageView.backgroundColor = UIColor.background
        
        contentView.addSubview(bgView)
        bgView.addSubview(averageView)
        
        bgView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
        }

        averageView.snp.makeConstraints { make in
            make.trailing.leading.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
        }
        
    
        
    }


}

open class TitleStackView: UIStackView {
    
    open var font: UIFont = Fonts.systemFont(ofSize: 10) {
        didSet {
            for label in self.labels {
                label.font = font
            }
        }
    }
    open var textColor: UIColor = UIColor.black {
        didSet {
            for label in self.labels {
                label.textColor = textColor
            }
        }
    }
    
    open var labels: [UILabel] {
        return self.subviews.compactMap {
            return $0 as? UILabel
        }
    }
    
   
    open var titles: [String]? {
        willSet {
            guard let newValue = newValue else {
                return
            }
            
            if titles == newValue {
                return
            }
            
            for view in self.subviews {
                view.removeFromSuperview()
            }
            
            _ = newValue.map {[weak self] (text) ->UILabel in
                
                let label = UILabel()
                label.text = text
                label.font = self?.font
                label.textColor = self?.textColor
                
                self?.addArrangedSubview(label)
                
                return label
            }
        }

    }
    
    public required init() {
        super.init(frame: CGRect.zero)
        
        makeUI()
        
    }
    
    public required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func makeUI() {
        
        distribution = .equalSpacing
        
    }
}

open class ChartLineView: View {
    
    public var lineColor: UIColor = UIColor.assist_EEEEEE
    public var bottomLineColor: UIColor = UIColor.assist_EEEEEE
   
    public override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
    }
    
    open override func makeUI() {
        addHorizontal()
        
    }
    
    open func addHorizontal() {
        let space = (self.frame.height - 1)/4.0
        var beforeLine: LineView?
        
        for i in 0 ..< 5 {
            let line = LineView()
            addSubview(line)
            
            line.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                
                if let beforeLine = beforeLine {
                    make.centerY.equalTo(beforeLine.snp.centerY).offset(-space)
                } else {
                    make.bottom.equalTo(self.snp.bottom)
                }
                make.height.equalTo(1.0)
            }
            
            beforeLine = line
            
        }
    }
    
    open func setLineColor(lineColor: UIColor, bottomLineColor: UIColor){
        addHorizontal()
    }
    
    
    open class LineView: View {
        
    
        public let shapeLayer = CAShapeLayer()
        
        open override func makeUI() {
            
            shapeLayer.fillColor = UIColor.clear.cgColor
            shapeLayer.strokeColor = UIColor.assist_EEEEEE.cgColor
            shapeLayer.lineDashPattern = [3.0, 3.0]
            shapeLayer.lineJoin = .round
            
            self.layer.addSublayer(shapeLayer)
            
        }
        
        var path: UIBezierPath {
            
            let path = UIBezierPath()
            path.move(to: CGPoint(x: 0, y: self.frame.height/2.0))
            path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height/2.0))
            return path
        }
        
        
        open override func layoutSubviews() {
            super.layoutSubviews()
            
            self.shapeLayer.path = self.path.cgPath
        
        }
        
        
    }
    
}


